import * as Cesium from "cesium"
import cesiumExt from "@/cMap/plugins/uitls/cesiumExt/cesiumExt"
const  modelClick = function (viewer,datasourses) {
    this.viewer = viewer
    this.datasourses = datasourses
    this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
}
modelClick.prototype = {
    reshDatasourses:function(datasourses){
        this.datasourses = datasourses
    },
    initEvt:function(){
        var self = this
        var viewer = this.viewer 
         this.handler.setInputAction(function (movement) {
            //测试
            var pick = viewer.scene.pick(movement.position);
            if (Cesium.defined(pick) && pick.id) {
                var datasourses = self.datasourses
                if(datasourses && datasourses.entities && datasourses.entities.contains(pick.id)){
                    var wp = movement.position;
                    if (!Cesium.defined(wp)) {
                        return;
                    }
                    var ray = viewer.camera.getPickRay(wp);
                    if (!Cesium.defined(ray)) {
                        return;
                    }
                    var cartesian = viewer.scene.globe.pick(ray, viewer.scene);
                    if (!Cesium.defined(cartesian)) {
                        return;
                    }
                    var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
                    var lnglat = {
                        lon:cartographic.longitude / Math.PI * 180,
                        lat:cartographic.latitude / Math.PI * 180
                    }
                    var content =`<ul class="win_info">
                        <li><strong>站点名称:</strong>${pick.id._properties._STNM._value}</li>
                        <li><strong>所属流域:</strong>${pick.id._properties._HNNM._value}</li>
                        <li><strong>站点地址:</strong>${pick.id._properties._STLC._value}</li>
                        </ul>`

                    var position =  Cesium.Cartographic.fromDegrees(lnglat.lon,lnglat.lat);
                    var promise = Cesium.sampleTerrain(self.viewer.terrainProvider,12,[position]);
                    Cesium.when(promise, function(updatedPositions) {
                        var height = updatedPositions[0].height;
                        var hpoint = new Cesium.Cartesian3.fromDegrees(lnglat.lon,lnglat.lat,height);
                        self._openinfoWindow({position:hpoint},content)
                    });
                }
            }else{
                self._closeinfoWindow()
            }

        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    },
    _openinfoWindow:function(position,content){
        
        var self = this
        var infoTmep =  '<div class="line"></div>'+
            '<div class="main">'+
            '</div>'
        var InfoWinDom = document.getElementById("cesium-infoWindows")
        if(!InfoWinDom){
            InfoWinDom = document.createElement("div")
            InfoWinDom.innerHTML = infoTmep
            InfoWinDom.className = "ysc-dynamic-layer"
            InfoWinDom.id = "cesium-infoWindows"
            var ViewDom = self.viewer.container
            ViewDom.appendChild(InfoWinDom);
        }
        var elment = $(InfoWinDom);
        var data={
            layerId:"layer1",//英文，且唯一,内部entity会用得到
            position:position.position,
            element:elment
        };
        cesiumExt.showDynamicLayer(self.viewer,data,function (){ //回调函数 改变弹窗的内容;
            elment.find(".main").html(content);
        });
    },
    _closeinfoWindow:function(){
        var InfoWinDom = document.getElementById("cesium-infoWindows")
        if(InfoWinDom){
            $(InfoWinDom).remove()
        }
    }
}
export default modelClick
       